﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BneyBaruch.Ingest.MorningLesson.Services;
using BneyBaruch.Ingest.MorningLesson.Services.Data;
using System.ServiceModel;
using System.Windows.Forms;
using System.Threading;
using BneyBaruch.Ingest.Utils.FileSystem;
using BneyBaruch.Ingest.Utils;

namespace BneyBaruch.Ingest.MorningLesson.Recorder.Services
{
    [ServiceBehavior
        (
            InstanceContextMode = InstanceContextMode.Single,
            ConcurrencyMode = ConcurrencyMode.Reentrant
        )
    ]
    public class RecordingController : IRecordingController
    {
        #region Constants

        // TODO: D drive in work not exists therefore it changed to L,change in production to d
        private const string DriveD = "l";
        // TODO: E drive in work not exists therefore it changed to L,change in production to e
        private const string DriveE = "k";

        #endregion

        #region IRecordingController Members

        public void RequestMatrixAdjustment(RequestMatrixAdjustmentRequest requestMatrixAdjustmentRequest)
        {
            IRecordingControllerCallback caller = OperationContext.Current.GetCallbackChannel<IRecordingControllerCallback>();
            // Request user in another thread without blocking wcf communication and callback to caller in new thread.
            ThreadPool.QueueUserWorkItem(
                delegate(Object state)
                {
                    MessageBox.Show("Approve matrix adjustment");
                    caller.ApproveMatrixAdjustment(
                        new MatrixAdjustmentApprovalStatus()
                        {
                            WorkflowInstanceId = requestMatrixAdjustmentRequest.WorkflowInstanceId
                        }
                    );
                }
            );
        }

        public void PrepareFileSystemBeforeRecording(PrepareFileSystemBeforeRecordingRequest prepareFileSystemBeforeRecordingRequest)
        {
            IRecordingControllerCallback caller = OperationContext.Current.GetCallbackChannel<IRecordingControllerCallback>();
            // Request user in another thread without blocking wcf communication and callback to caller in new thread.
            ThreadPool.QueueUserWorkItem(
                delegate(Object state)
                {
                    MessageBox.Show("Prepare file system on recording controller started.");
                    FileSystemCleanData fileSystemDriveDCleanData = DriveUtil.CleanFileSystemIfNeeded(DriveD, Constants.ThirtyGB, DriveD + ":");
                    FileSystemCleanData fileSystemDriveECleanData = DriveUtil.CleanFileSystemIfNeeded(DriveE, Constants.ThirtyGB, DriveE + ":");
                    FileSystemCleanData fileSystemCleanData = new FileSystemCleanData()
                    {
                        IsCleaned = fileSystemDriveDCleanData.IsCleaned || fileSystemDriveECleanData.IsCleaned,
                        FreedItems = new List<FileSystemCleanItem>(fileSystemDriveDCleanData.FreedItems.Count + fileSystemDriveECleanData.FreedItems.Count)
                    };
                    fileSystemCleanData.FreedItems.AddRange(fileSystemDriveDCleanData.FreedItems);
                    fileSystemCleanData.FreedItems.AddRange(fileSystemDriveECleanData.FreedItems);
                    caller.ReportFileSystemStatus(
                        new FileSystemStatus()
                        {
                            WorkflowInstanceId = prepareFileSystemBeforeRecordingRequest.WorkflowInstanceId,
                            FileSystemCleanStatistics = fileSystemCleanData
                        }
                    );
                }
            );
        }

        #endregion
    }
}
